/**
 *
 * @author User
 */

public class SettingsDialog extends javax.swing.JDialog {

  /**
   * Creates new form Settings
   */
  public SettingsDialog(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    for(int a=0;a<App.delays.length;a++) {
      speed.addItem(Integer.toString(App.delays[a]) + "ms");
    }
    speed.setSelectedIndex(2);
    loadSettings();
  }

  /**
   * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method
   * is always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jLabel1 = new javax.swing.JLabel();
    speed = new javax.swing.JComboBox<>();
    ok = new javax.swing.JButton();
    cancel = new javax.swing.JButton();
    jLabel2 = new javax.swing.JLabel();
    cont = new javax.swing.JCheckBox();
    duration = new javax.swing.JSpinner();
    jLabel3 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    triggerReset = new javax.swing.JSpinner();
    jLabel5 = new javax.swing.JLabel();
    jButton1 = new javax.swing.JButton();
    triggerTag = new javax.swing.JLabel();
    SOCKSEnable = new javax.swing.JCheckBox();
    SOCKSHost = new javax.swing.JTextField();
    jLabel6 = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Settings");

    jLabel1.setText("Speed");

    speed.setMaximumSize(new java.awt.Dimension(100, 32767));

    ok.setText("OK");
    ok.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okActionPerformed(evt);
      }
    });

    cancel.setText("Cancel");
    cancel.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        cancelActionPerformed(evt);
      }
    });

    jLabel2.setText("Duration (s)");

    cont.setText("Continuous");
    cont.addItemListener(new java.awt.event.ItemListener() {
      public void itemStateChanged(java.awt.event.ItemEvent evt) {
        contItemStateChanged(evt);
      }
    });

    duration.setModel(new javax.swing.SpinnerNumberModel(1, 1, 86400, 1));

    jLabel3.setText("Trigger Set");

    jLabel4.setText("Trigger Reset");

    triggerReset.setModel(new javax.swing.SpinnerNumberModel(250, 10, 1000, 10));

    jLabel5.setText("ms");

    jButton1.setText("Edit");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });

    triggerTag.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
    triggerTag.setText("...");

    SOCKSEnable.setText("Use SOCKS4 Server");

    jLabel6.setText("Host");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(cancel)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(ok))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
              .addComponent(jLabel2)
              .addComponent(jLabel1)
              .addComponent(jLabel3)
              .addComponent(jLabel4))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(triggerTag, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
              .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                  .addComponent(speed, 0, 100, Short.MAX_VALUE)
                  .addComponent(duration))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cont))
              .addGroup(layout.createSequentialGroup()
                .addComponent(triggerReset)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel5))
              .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
          .addGroup(layout.createSequentialGroup()
            .addComponent(SOCKSEnable)
            .addGap(0, 0, Short.MAX_VALUE))
          .addGroup(layout.createSequentialGroup()
            .addComponent(jLabel6)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(SOCKSHost)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(speed, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel2)
          .addComponent(duration, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(cont))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel3)
          .addComponent(jButton1))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(triggerTag)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel4)
          .addComponent(triggerReset, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel5))
        .addGap(18, 18, 18)
        .addComponent(SOCKSEnable)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(SOCKSHost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(jLabel6))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 102, Short.MAX_VALUE)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(ok)
          .addComponent(cancel))
        .addContainerGap())
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void contItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_contItemStateChanged
    boolean c = cont.isSelected();
    if (c) {
      duration.setValue(10);
      duration.setEnabled(false);
    } else {
      duration.setEnabled(true);
    }
  }//GEN-LAST:event_contItemStateChanged

  private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
    dispose();
  }//GEN-LAST:event_cancelActionPerformed

  private void okActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okActionPerformed
    saveSettings();
    dispose();
  }//GEN-LAST:event_okActionPerformed

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    TagDialog dialog = new TagDialog(null, true);
    dialog.load(App.trigger);
    dialog.bitOnly();
    dialog.setVisible(true);
    if (dialog.accepted()) {
      dialog.save(App.trigger);
      triggerTag.setText(App.trigger.toString());
    }
  }//GEN-LAST:event_jButton1ActionPerformed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JCheckBox SOCKSEnable;
  private javax.swing.JTextField SOCKSHost;
  private javax.swing.JButton cancel;
  private javax.swing.JCheckBox cont;
  private javax.swing.JSpinner duration;
  private javax.swing.JButton jButton1;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JButton ok;
  private javax.swing.JComboBox<String> speed;
  private javax.swing.JSpinner triggerReset;
  private javax.swing.JLabel triggerTag;
  // End of variables declaration//GEN-END:variables


  void loadSettings() {
    speed.setSelectedIndex(App.speed);
    boolean c = App.duration == -1;
    cont.setSelected(c);
    if (c) {
      duration.setValue(10);
      duration.setEnabled(false);
    } else {
      duration.setValue(App.duration);
      duration.setEnabled(true);
    }
    //load trigger tags
    triggerTag.setText(App.trigger.toString());
    triggerReset.setValue(App.triggerReset);
    SOCKSEnable.setSelected(App.SOCKSEnable);
    SOCKSHost.setText(App.SOCKSHost);
  }

  void saveSettings() {
    App.speed = speed.getSelectedIndex();
    if (cont.isSelected()) {
      App.duration = -1;
    } else {
      App.duration = (Integer)duration.getValue();
    }
    App.triggerReset = (Integer)triggerReset.getValue();
    App.SOCKSEnable = SOCKSEnable.isSelected();
    App.SOCKSHost = SOCKSHost.getText();
  }
}
